package com.chinasofti.dao;

import com.chinasofti.util.ConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CounterDao {
    private Connection conn;

    public void updatevisitcount(int visitcount){
        conn = ConnectionFactory.getConnection();
        String sql = "update counter set visitcount=?";
        PreparedStatement pstm = null;
        try {
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1,visitcount);
            pstm.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
    }


    public int selectvisitcount(){
        int visitcount = 0;
        conn = ConnectionFactory.getConnection();
        String sql = "select visitcount from counter";
        PreparedStatement pstm = null;

        try {
            pstm = conn.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            if(rs.next()){
                visitcount = rs.getInt("visitcount");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
        return visitcount;
    }

}